﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Persistencia
{
    public class FileSystemElementPersistence
    {
        public FileSystemElement InsertarFileSystemElement(FileSystemElement fse)
        {
            using (SearchDBEntities mydb = new SearchDBEntities())
            {
                mydb.FileSystemElement.AddObject(fse);
                mydb.SaveChanges();
                return fse;
            }
        }

        public void EliminarFileSystemElement(int idFse)
        {
            using (SearchDBEntities mydb = new SearchDBEntities())
            {
                mydb.DeleteObject(mydb.FileSystemElement.First(f => f.Id == idFse));
                mydb.SaveChanges();
            }
        }

        public void ModificarFileSystemElement(int idFse, FileSystemElement fseNuevo)
        {
            using (SearchDBEntities mydb = new SearchDBEntities())
            {
                FileSystemElement fse = mydb.FileSystemElement.FirstOrDefault(f => f.Id == idFse);
                fse.Tiene_Subcarpetas = fseNuevo.Tiene_Subcarpetas;
                fse.Path = fseNuevo.Path;
                fse.Is_File = fseNuevo.Is_File;
                fse.Extension = fseNuevo.Extension;
                fse.Usuario_Ultima_Modificacion = fseNuevo.Usuario_Ultima_Modificacion;
                fse.Fecha_Ultima_Modificacion = fseNuevo.Fecha_Ultima_Modificacion;
                Usuario user = mydb.Usuario.FirstOrDefault(u => u.Id == fseNuevo.Usuario_Ultima_Modificacion);
                fse.Usuario1Reference.Value = user;
                mydb.SaveChanges();
            }
        }

        public void ModificarFileSystemElementTamano(int idFse, int tamano)
        {
            using (SearchDBEntities mydb = new SearchDBEntities())
            {
                FileSystemElement fse = mydb.FileSystemElement.FirstOrDefault(f => f.Id == idFse);
                fse.Tamaño = tamano;
                mydb.SaveChanges();
            }
        }

        public List<FileSystemElement> SelectAllFileSystemElements()
        {
            using (SearchDBEntities mydb = new SearchDBEntities())
            {
                List<FileSystemElement> fseList = mydb.FileSystemElement.ToList();
                return fseList;
            }
        }

        public List<FileSystemElement> GetFileSystemElementsMonitoreados()
        {
            using (SearchDBEntities mydb = new SearchDBEntities())
            {
                var query = from f in mydb.FileSystemElement
                            where f.Monitoreado.Equals(true)
                            select f;
                return query.ToList();
            }
        }

        public List<FileSystemElement> BuscarFSE(string termino)
        {
            using (SearchDBEntities mydb = new SearchDBEntities())
            {
                DateTime fecha;
                bool esFecha = DateTime.TryParse(termino, out fecha);
                if (esFecha){
                    var query = from f in mydb.FileSystemElement
                                where f.Fecha_Creacion == fecha 
                                || f.Path.StartsWith(termino)
                                || f.Nombre.StartsWith(termino)
                                || f.Extension.StartsWith(termino)
                                select f;
                    return query.ToList();
                }
                else {
                    var query = from f in mydb.FileSystemElement
                                where f.Path.StartsWith(termino)
                                || f.Nombre.StartsWith(termino)
                                || f.Extension.StartsWith(termino)
                                select f;
                    return query.ToList();
                }
            }
        }

        public FileSystemElement ObtenerFileSystemElement(int idFsElement)
        {
            FileSystemElement fileSystemElement;
            using (SearchDBEntities mydb = new SearchDBEntities())
            {
                fileSystemElement = mydb.FileSystemElement.FirstOrDefault(fse => fse.Id == idFsElement);
            }
            return fileSystemElement;
        }

        public FileSystemElement ObtenerFileSystemElementByFullPath(string fullPath)
        {
            string nombre = Path.GetFileNameWithoutExtension(fullPath);
            string path = Path.GetDirectoryName(fullPath);
            string extension = "";
                extension = Path.GetExtension(fullPath);
            FileSystemElement fileSystemElement;
            using (SearchDBEntities mydb = new SearchDBEntities())
            {
                //fileSystemElement = mydb.FileSystemElement.FirstOrDefault(fse => fse.Path == path && fse.Nombre == nombre && fse.Extension == extension);
                var query = from f in mydb.FileSystemElement
                            where f.Path == path
                                || f.Nombre == nombre
                                || f.Extension == extension
                            select f;
                fileSystemElement = query.ToList().ElementAt(0);
                return fileSystemElement;
            }
        }
    }
}
